<style scoped lang="scss">
@import url('index.scss');
</style>

<template>
  <div class="app-container">
    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="医生名称" prop="dname">
              <el-input v-model="queryParams.dname" placeholder="请输入医生名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="身份证号" prop="dcard">
              <el-input v-model="queryParams.dcard" placeholder="请输入身份证号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="出生日期" prop="dbirthday">
        <el-input
          v-model="queryParams.dbirthday"
          placeholder="请输入出生日期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="医生工号" prop="dno">
              <el-input v-model="queryParams.dno" placeholder="请输入医生工号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="dmobile">
              <el-input v-model="queryParams.dmobile" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="社保卡号" prop="dssid">
        <el-input
          v-model="queryParams.dssid"
          placeholder="请输入社保卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <!-- <el-form-item label="教育状况" prop="dedulevel">
        <el-input
          v-model="queryParams.dedulevel"
          placeholder="请输入教育状况"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="职称" prop="protitle">
              <!-- <el-input v-model="queryParams.protitle" placeholder="请输入职称" clearable @keyup.enter="handleQuery" /> -->
               <el-select v-model="queryParams.protitle" placeholder="请选择职称" clearable @keyup.enter="handleQuery" style="width: 208px;">
                <el-option v-for="dict in ha_doctor_protitle" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="执业证号" prop="licenseno">
              <el-input v-model="queryParams.licenseno" placeholder="请输入执业证号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="医师级别" prop="dlevel">
              <!-- <el-input v-model="queryParams.dlevel" placeholder="请输入医师级别" clearable @keyup.enter="handleQuery" /> -->
               <el-select v-model="queryParams.dlevel" placeholder="请选择医师级别" clearable @keyup.enter="handleQuery" style="width: 208px;">
                <el-option v-for="dict in ha_doctor_level" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="所在院区" prop="dhoscampus">
              <el-input v-model="queryParams.dhoscampus" placeholder="请输入所在院区" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所在科室" prop="deptcode">
              <!-- <el-input v-model="queryParams.deptcode" placeholder="请输入所在科室" clearable @keyup.enter="handleQuery" /> -->
               <el-select v-model="queryParams.deptcode" placeholder="请选择所在科室" clearable @keyup.enter="handleQuery" style="width: 208px;">
                <el-option v-for="dict in departmentList" :key="dict.kid" :label="dict.kname" :value="dict.kid" />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button class="newButton" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:doctors:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:doctors:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:doctors:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:doctors:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:doctors:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:doctors:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:doctors:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:doctors:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <div class="newtable">
      <el-table v-loading="loading" :data="doctorsList" @selection-change="handleSelectionChange"
        :cell-style="{ backgroundColor: '#f9f9f9' }">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="expand" width="55" label="展开">
          <template #default="props">
            <div class="detail">
              <el-row :gutter="20">
                <el-col :span="8">
                  <p><b>职称：</b>{{ ha_doctor_protitle.find(item => item.value == props.row.protitle)?.label || '未知' }} </p>
                </el-col>
                <el-col :span="8">
                  <p><b>教育：</b> {{ ha_edu_level.find(item => item.value == props.row.dedulevel)?.label || '未知' }} </p>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <p><b>技能简介：</b>{{ props.row.dskill }}</p>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <p><b>详细介绍：</b>{{ props.row.ddescription }}</p>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="ID" width="55" align="center" prop="did" />
        <el-table-column label="医生名称" align="center" prop="dname" />
               <el-table-column label="医生头像" align="center" prop="dimage" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.dimage" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="身份证号" min-width="160" align="center" prop="dcard" />
        <el-table-column label="出生日期" align="center" prop="dbirthday" />
        <el-table-column label="年龄" width="55" align="center" prop="age" />
        <el-table-column label="医生工号" align="center" prop="dno" />
        <el-table-column label="性别" align="center" prop="dsex">
          <template #default="scope">
            <span v-if="scope.row.dsex == '0'">男</span>
            <span v-else-if="scope.row.dsex == '1'">女</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="dmobile" />
        <!-- <el-table-column label="社保卡号" align="center" prop="dssid" /> -->
        <!-- <el-table-column label="教育状况" align="center" prop="dedulevel">
          <template #default="scope">
            <span>{{ ha_edu_level.find(item => item.value == scope.row.dedulevel)?.label || '未知' }}</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="职称" align="center" prop="protitle">
          <template #default="scope">
            <span>{{ ha_doctor_protitle.find(item => item.value == scope.row.protitle)?.label || '未知' }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="执业证号" min-width="160" align="center" prop="licenseno" />
        <el-table-column label="医师级别" align="center" prop="dlevel">
          <template #default="scope">
            <span>{{ ha_doctor_level.find(item => item.value == scope.row.dlevel)?.label || '未知' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所在院区" align="center" prop="dhoscampus" />
        <el-table-column label="所在科室" align="center" prop="deptcode">
          <template #default="scope">
            <span>{{ departmentList.find(item => item.kid == scope.row.deptcode)?.kname || '未知' }}</span>
          </template>
        </el-table-column>
        <!--  <el-table-column label="技能介绍" align="center" prop="dskill" />
        <el-table-column label="详细介绍" align="center" prop="ddescription" /> -->
        <el-table-column label="状态" align="center" prop="status" >
          <template #default="scope">
           <el-tag :type="scope.row.status == '1' ? 'success' : 'danger'">{{ scope.row.status == '1' ? '正常' : '停用'
              }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:doctors:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:doctors:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 添加或修改医生列表对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="doctorsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医生头像" prop="dimage">
          <image-upload v-model="form.dimage" />
        </el-form-item>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="医生姓名" prop="dname">
              <el-input v-model="form.dname" placeholder="请输入医生姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="dsex">
              <!-- <el-radio-group v-model="form.dsex">
                <el-radio v-for="item in sys_user_sex" :label="item.value" :key="item.value">{{ item.label }}</el-radio>
              </el-radio-group> -->
              <el-select v-model="form.dsex" placeholder="请选择性别" clearable>
                <el-option v-for="item in sys_user_sex" :label="item.label" :value="item.value" :key="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="身份证号" prop="dcard">
              <el-input v-model="form.dcard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="dbirthday">
              <!-- <el-input v-model="form.dbirthday" placeholder="请输入出生日期" /> -->
              <el-date-picker clearable v-model="form.dbirthday" type="date" value-format="YYYY-MM-DD" disabled
                placeholder="请选择出生年月" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="医生工号" prop="dno">
              <el-input v-model="form.dno" placeholder="请输入医生工号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社保卡号" prop="dssid">
              <el-input v-model="form.dssid" placeholder="请输入社保卡号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="执业证号" prop="licenseno">
              <el-input v-model="form.licenseno" placeholder="请输入执业证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="dmobile">
              <el-input v-model="form.dmobile" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="教育状况" prop="dedulevel">
              <!-- <el-input v-model="form.dedulevel" placeholder="请输入教育状况" /> -->
              <el-select v-model="form.dedulevel" placeholder="请选择教育状况" clearable>
                <el-option v-for="item in ha_edu_level" :label="item.label" :value="item.value" :key="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职称" prop="protitle">
              <!-- <el-input v-model="form.protitle" placeholder="请输入职称" /> -->
              <el-select v-model="form.protitle" placeholder="请选择职称" clearable>
                <el-option v-for="item in ha_doctor_protitle" :label="item.label" :value="item.value"
                  :key="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="医师级别" prop="dlevel">
              <!-- <el-input v-model="form.dlevel" placeholder="请输入医师级别" /> -->
              <el-select v-model="form.dlevel" placeholder="请选择医师级别" clearable>
                <el-option v-for="item in ha_doctor_level" :label="item.label" :value="item.value" :key="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="所在院区" prop="dhoscampus">
              <el-input v-model="form.dhoscampus" placeholder="请输入所在院区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在科室" prop="deptcode">
              <!-- <el-input v-model="form.deptcode" placeholder="请输入所在科室" /> -->
              <el-select v-model="form.deptcode" placeholder="请选择所在科室" clearable>
                <el-option v-for="item in departmentList" :key="item.kid" :label="item.kname" :value="item.kid" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="15">
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>-->
        <el-form-item label="技能介绍" prop="dskill">
          <el-input v-model="form.dskill" placeholder="请输入技能介绍" />
        </el-form-item>
        <el-form-item label="详细介绍" prop="ddescription">
          <!-- <el-input v-model="form.ddescription" type="textarea" placeholder="请输入内容" /> -->
          <el-input type="textarea" v-model="form.ddescription" placeholder="请输入详细介绍" rows="6" />
        </el-form-item>
                <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in ha_normal_status" :key="dict.value" :value="dict.value" :label="dict.value">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Doctors">
import { listDoctors, getDoctors, delDoctors, addDoctors, updateDoctors } from "@/api/system/doctors"
import { listDepartment } from "@/api/system/department"

const { proxy } = getCurrentInstance()
//性别字典
const { sys_user_sex } = proxy.useDict('sys_user_sex')
const { ha_normal_status } = proxy.useDict('ha_normal_status'); // 使用字典
const { ha_edu_level } = proxy.useDict('ha_edu_level'); 
const { ha_doctor_level } = proxy.useDict('ha_doctor_level'); 
const { ha_doctor_protitle } = proxy.useDict('ha_doctor_protitle'); 


const doctorsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dname: null,
    dcard: null,
    dbirthday: null,
    dno: null,
    dsex: null,
    dmobile: null,
    dssid: null,
    dedulevel: null,
    protitle: null,
    licenseno: null,
    dlevel: null,
    dhoscampus: null,
    deptcode: null,
    dimage: null,
    dskill: null,
    ddescription: null,
    status: null
  },
  rules: {
    dname: [{ required: true, message: '请输入医生姓名', trigger: 'blur' }],
    // dcard: [{ required: true, message: '请输入医生身份证号', trigger: 'blur' }],
    dcard: [{ required: true, message: '请输入医生身份证号', trigger: 'blur' },
    {
        pattern: /^[1-9]\d{5}(19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i,
        message: '请输入正确的身份证号',
        trigger: 'blur',
    }],
    dno: [{ required: true, message: '请输入医生工号', trigger: 'blur' }],
    dmobile: [{ required: true, message: '请输入手机号', trigger: 'blur' },
            {
        pattern: /^1[3-9]\d{9}$/,
        message: '请输入正确的手机号',
        trigger: 'blur',
      },
    ],
    dssid: [{ required: true, message: '请输入社保卡号', trigger: 'blur' }],
    licenseno: [{ required: true, message: '请输入执业证号', trigger: 'blur' }],
    dedulevel: [{ required: true, message: '请输入教育状况', trigger: 'blur' }],
    protitle: [{ required: true, message: '请输入职称', trigger: 'blur' }],
    dlevel: [{ required: true, message: '请输入医师级别', trigger: 'blur' }],
    dhoscampus: [{ required: true, message: '请输入所在院区', trigger: 'blur' }],
    deptcode: [{ required: true, message: '请输入所在科室', trigger: 'blur' }],

  }
})

const { queryParams, form, rules } = toRefs(data)
const newqueryParams = {
  pageNum: 1,
  pageSize: 1000,
}
// 获取科室列表
const departmentList = ref([])

onMounted(async () => {
  //列出所有的科室
  getDepartmentList()
//end
})
//根据身份证号自动带出出生年月
watch(() => form.value.dcard, extractBirthDate);

function extractBirthDate() {
  const idCard = form.value.dcard;
  if (idCard && idCard.length === 18) {
    const year = idCard.substring(6, 10);
    const month = idCard.substring(10, 12);
    const day = idCard.substring(12, 14);
    form.value.dbirthday = `${year}-${month}-${day}`;
  } else {
    form.value.dbirthday = null;
  }
}

// 获取科室列表@鲍小伟20251111
function getDepartmentList() {
  listDepartment(newqueryParams).then(response => {
    console.log(response)
    if(response.code == 200){
      departmentList.value = response.rows
    } else {
      departmentList.value = []
    }
  })
}

/** 查询医生列表列表 */
function getList() {
  loading.value = true
  listDoctors(queryParams.value).then(response => {
    // 计算年龄鲍小伟
    response.rows.forEach(item => {
      item.age = proxy.$filters.age(item.dbirthday)
    })
    doctorsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    did: null,
    dname: null,
    dcard: null,
    dbirthday: null,
    dno: null,
    dsex: null,
    dmobile: null,
    dssid: null,
    dedulevel: null,
    protitle: null,
    licenseno: null,
    dlevel: null,
    dhoscampus: null,
    deptcode: null,
    dimage: null,
    dskill: null,
    ddescription: null,
    status: '1'
  }
  proxy.resetForm("doctorsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.did)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加医生列表"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _did = row.did || ids.value
  getDoctors(_did).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改医生列表"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["doctorsRef"].validate(valid => {
    if (valid) {
      if (form.value.did != null) {
        updateDoctors(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDoctors(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _dids = row.did || ids.value
  proxy.$modal.confirm('是否确认删除医生列表编号为"' + _dids + '"的数据项？').then(function() {
    return delDoctors(_dids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/doctors/export', {
    ...queryParams.value
  }, `doctors_${new Date().getTime()}.xlsx`)
}

getList()
</script>
